package com.dubbo.gmail.controller;

import com.alibaba.dubbo.rpc.service.EchoService;
import com.dubbo.gmail.ConsumerApplication;
import com.dubbo.gmail.api.config.MatchServerConfig;
import com.dubbo.gmail.api.message.RequestDubboFactory;
import com.dubbo.gmail.api.message.impl.UserMatchRequest;
import com.dubbo.gmail.api.model.UserModel;
import com.dubbo.gmail.api.service.MatchRequestService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @ResponseBody
    @RequestMapping("/addMatch")
    public String addMatch(@RequestParam("id") int id, @RequestParam("name") String name) {
        MatchRequestService matchRequestService = RequestDubboFactory.buildRequestDubbo(MatchRequestService.class,
                ConsumerApplication.getApplicationContext(), MatchServerConfig.getInstance().getUrl());

        EchoService echoService = (EchoService) matchRequestService;

        // 回声测试可用性
        String status = (String) echoService.$echo("OK");

        assert (status.equals("OK"));

        return matchRequestService.addMatch(new UserMatchRequest(id, name));
    }
}
